package com.hx.guava;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.ExecutionException;

/**
 * @author Minghua Chen
 * @date 2020-09-20 21:59
 */
public class GuavaDemo {
    private static final Logger logger = LoggerFactory.getLogger(GuavaDemo.class);

    public static void main(String[] args) throws ExecutionException {
        LoadingCache<String, String> lc = CacheBuilder.newBuilder().build(new CacheLoader<String, String>() {
            @Override
            public String load(String key) throws Exception {
                return slowMethod(key);
            }
        });
        String hello = lc.get("hello");
        logger.info(hello);
    }

    private static String slowMethod(String key) throws Exception {
        Thread.sleep(100);
        return key + ".result";
    }
}
